#define  _CRT_SECURE_NO_WARNINGS
class Solution {
    long long transform(int dif, int cur, int l, int r)
    {
        long long len = r - l + 1;
        long long sum = cur * len;
        return sum - dif;
    }
public:
    int maxFrequency(vector<int>& nums, int k)
    {
        sort(nums.begin(), nums.end());
        int ret = 0;
        int l = 0, r = 0;
        int cur = nums[l];
        int dif = 0;
        int n = nums.size();
        while (r < n)
        {
            dif += nums[r] - cur;
            // cout << "l " << l << "r " << r << endl;
            // cout << "transform(dif, nums[r] - cur, l, r):" << transform(dif, nums[r] - cur, l, r) << endl;
            while (transform(dif, nums[r] - cur, l, r) > k)
            {
                l++;
                dif -= nums[l] - cur;
                dif -= (r - l) * (nums[l] - cur);
                cur = nums[l];
            }

            r++;
            ret = max(ret, r - l);
        }
        return ret;
    }
};
//     1 4 8 13
//     0 3 7
// 21 - 10 = 11


// 2
//     3 6 9
// 3   0 3
// 6     3

// 3
//     1 2 3 4
// 1   0 1 2
// 2     0 1 2

// 3
//     1 2 2 3
// 1   0 1 1 2

// 2     0 0 1